package com.yanqu.road.server.manger.activity.businessreunion;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.businessreunion.BusinessReunionActivityModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：zfr
 * @date ：2022/4/18 10:14
 * 商海重逢
 */
public class BusinessReunionActivityMgr extends TempMgr {
    //配置
    private static ReunionConfig config;

    private static ActivityInfo activityInfo;

    public static ReunionConfig getConfig() {
        return config;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    public static void reloadActivityData() {
        getLogger().info("reload businessReunion activity start");
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.BusinessReunion.getValue());
        if (openActivityInfoList.size() <= 0) {
            getLogger().info("reload businessReunion activity finish, no activity in show time.");
            config = null;
            activityInfo = null;
            return;
        }
        ActivityInfo tempActivityInfo = openActivityInfoList.get(0);

        boolean activityChange = false;
        if (activityInfo == null || activityInfo.getActivityId() != tempActivityInfo.getActivityId()) {
            activityChange = true;
        }
        activityInfo = tempActivityInfo;
        List<Integer> activityIdList = new ArrayList<>();
        activityIdList.add(activityInfo.getActivityId());
        Map<String, ActivityConfig> activityConfigMap = ActivityBussiness.getActivityConfigMap(activityIdList).get(activityInfo.getActivityId());
        config = new ReunionConfig(activityConfigMap);

        if (activityChange) {
            BusinessReunionMgr.reloadActivityData(activityInfo.getActivityId());
            for (GamePlayer gamePlayer : GamePlayerMgr.getAllOnlinePlayer()) {
                gamePlayer.getModule(BusinessReunionActivityModule.class).reloadActivityData(activityInfo.getActivityId());
            }
        }

        getLogger().info("reload businessReunion activity finish, activityId {}", activityInfo.getActivityId());
    }

    public static ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public static String generateRecallCode(long playerId) {
        return BusinessReunionMgr.tenToSixtyTwoHex(playerId);
    }

    @Override
    public boolean stop() {
        return true;
    }
}
